import { makeAutoObservable } from 'mobx';
import { request } from '@/request';

class LoginStore {
    username: string = '';

    mobile: string = '';

    password: string = '';

    imgpath: string = '';

    accesstoken: string = '226cf966-f9f4-4bac-be21-b03adc10504d';

    userinfo: {
        id: string;
        loginname: string;
        avatar_url: string;
    } | null = null;

    constructor() {
        makeAutoObservable(this);
    }

    setAccesstoken(accesstoken: string) {
        this.accesstoken = accesstoken;
    }

    async postAccesstoken() {
        const res: any = await request.post('/api/v1/accesstoken', {
            accesstoken: this.accesstoken,
        });
        if (res.success) {
            this.userinfo = {
                id: res.id,
                loginname: res.loginname,
                avatar_url: res.avatar_url,
            };
            localStorage.setItem('accesstoken', this.accesstoken);
        } else {
            localStorage.removeItem('accesstoken');
        }
        return res.success;
    }

    setUsername(username: string) {
        this.username = username;
    }

    setMobile(mobile: string) {
        this.mobile = mobile;
    }

    setPassword(password: string) {
        this.password = password;
    }

    async uploadImg(data: any) {
        setTimeout(() => {
            this.imgpath = '';
        }, 2000);
    }
}

const loginStore = new LoginStore();
export default loginStore;
